<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . "/database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

try {
  $hostname = HOST_NAME;
  $username = USER_NAME2;
  $password = PASSWORD2;
  $dbname = DATABASE_NAME2;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}


//$orm1 = classToOrm('produceselect');
try {
  $stmt = $myPdo->prepare("SELECT p.e_oid AS id, p.trackId as trackId FROM ns_Produce AS p 
                          ");
  $stmt->execute();
  
  //$myManager->start_t();
  $myPdo->beginTransaction();
  $i = 0;

  while ($i < $stmt->rowCount()) {
    $produce = $stmt->fetch(PDO::FETCH_OBJ);
    
    $sql1 = " SELECT pi.e_oid as id, pi.trackId FROM ns_ProducePurchaseItem AS pi 
              LEFT JOIN ns__ez_relation_produce_producepurchaseitem AS ppi ON (pi.e_oid = ppi.oid_b AND ppi.var_a = 'purchaseitems')
              WHERE ppi.oid_a = $produce->id";
    $stmt1 = $myPdo->prepare($sql1);
    $stmt1->execute();
    $j = 0;
    while ($j < $stmt1->rowCount()) {
      $tmp1 = $stmt1->fetch(PDO::FETCH_OBJ);
      $sql11 = "UPDATE ns_ProducePurchaseItem SET produce_trackId = '$produce->trackId' where e_oid = $tmp1->id";
      $myPdo->exec($sql11);
      
      $sql2 = "SELECT ps.e_oid as id FROM ns_ProducePurchaseItemSelect AS ps 
               LEFT JOIN ns__ez_relation_producepurchaseitem_producepurchaseitemselect AS ppis ON ps.e_oid = ppis.oid_b
               WHERE ppis.oid_a = $tmp1->id";
      $stmt2 = $myPdo->prepare($sql2);
      $stmt2->execute();
      $k = 0;
      while ($k < $stmt2->rowCount()) {
        $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
        $sql21 = "UPDATE ns_ProducePurchaseItemSelect SET purchase_trackId = '$tmp1->trackId' where e_oid = $tmp2->id";
        $myPdo->exec($sql21);
        $k++;
      }
      $j++;
    }
    
    $sql3 = " SELECT u.e_oid as id FROM ns_ProduceUser AS u 
              LEFT JOIN ns__ez_relation_produce_produceuser AS pu ON pu.oid_b = u.e_oid
              WHERE pu.oid_a = $produce->id";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $a = 0;
    while ($a < $stmt3->rowCount()) {
      $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      $sql31 = "UPDATE ns_ProduceUser SET produce_id = $produce->id where e_oid = $tmp3->id";
      $myPdo->exec($sql31);
      $a++;
    }
    
    $sql4 = " SELECT e.e_oid as id FROM ns_ProduceEvent AS e 
              LEFT JOIN ns__ez_relation_produce_produceevent AS pe ON pe.oid_b = e.e_oid
              WHERE pe.oid_a = $produce->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $b = 0;
    while ($b < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $sql41 = "UPDATE ns_ProduceEvent SET produce_trackId = '$produce->trackId' where e_oid = $tmp4->id";
      $myPdo->exec($sql41);
      $b++;
    }
    
    $sql5 = " SELECT t.e_oid as id FROM ns_ProduceTrack AS t 
              LEFT JOIN ns__ez_relation_produce_producetrack AS pt ON pt.oid_b = t.e_oid
              WHERE pt.oid_a = $produce->id";
    $stmt5 = $myPdo->prepare($sql5);
    $stmt5->execute();
    $c = 0;
    while ($c < $stmt5->rowCount()) {
      $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
      $sql51 = "UPDATE ns_ProduceTrack SET produce_id = $produce->id, parent_trackId = '$produce->trackId' where e_oid = $tmp5->id";
      $myPdo->exec($sql51);
      $c++;
    }
    
    $sql6 = " SELECT p.e_oid as id FROM ns_Produce AS p 
              LEFT JOIN ns__ez_relation_produce_produce AS pp ON pp.oid_b = p.e_oid
              WHERE pp.oid_a = $produce->id";
    $stmt6 = $myPdo->prepare($sql6);
    $stmt6->execute();
    $d = 0;
    while ($d < $stmt6->rowCount()) {
      $tmp6 = $stmt6->fetch(PDO::FETCH_OBJ);
      $sql61 = "UPDATE ns_Produce SET parent_trackId = '$produce->trackId' where e_oid = $tmp6->id";
      $myPdo->exec($sql61);
      $d++;
    }
    
    $sql7 = " SELECT n.e_oid as id FROM ns_ProduceNote AS n 
              LEFT JOIN ns__ez_relation_produce_producenote AS pn ON pn.oid_b = n.e_oid
              WHERE pn.oid_a = $produce->id";
    $stmt7 = $myPdo->prepare($sql7);
    $stmt7->execute();
    $e = 0;
    while ($e < $stmt7->rowCount()) {
      $tmp7 = $stmt7->fetch(PDO::FETCH_OBJ);
      $sql71 = "UPDATE ns_ProduceNote SET produce_trackId = '$produce->trackId' where e_oid = $tmp7->id";
      $myPdo->exec($sql71);
      $e++;
    }
    
    $i++;
  }
  $myPdo->commit();
  echo json_encode(array());
} catch (PDOException $e) {
  $myPdo->rollBack();
  echo $e->getMessage();
}
?>